package com.zstu.lvxiang.controller;

import com.zstu.lvxiang.config.AccessLimit;
import com.zstu.lvxiang.utils.DateUtils;
import com.zstu.lvxiang.vo.RecordVo;
import com.zstu.lvxiang.pojo.Records;
import com.zstu.lvxiang.service.IRecordService;
import com.zstu.lvxiang.vo.RespBean;
import com.zstu.lvxiang.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Trafalgar
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    IRecordService recordService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 添加每天学习情况
     *
     * @param recordsVo
     * @param sno
     * @return
     */
    @AccessLimit
    @PostMapping("/insert")
    public RespBean insert(@Valid RecordVo recordsVo,
                           @RequestAttribute("sno") String sno) {
        List<Records> records = new ArrayList<>();
        String date = DateUtils.dateToString(new Date(), DateUtils.DEFAULT_DATE_PATTERN);
        int[] categoryIds = recordsVo.getCategoryIds();
        Float[] times = recordsVo.getTimes();
        redisTemplate.delete("report_sys_" + sno + date);
        redisTemplate.delete("report_sys_" + sno + date + "rank");
        for (int i = 0; i < times.length; i++) {
            if (times[i] != null) {
                records.add(new Records(sno, categoryIds[i], times[i], date));
            }
        }
        if (recordService.saveBatch(records)) {
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }

    /**
     * 展示某天学习情况
     *
     * @param date
     * @param sno
     * @return
     */
    @AccessLimit
    @GetMapping("/dailyInfo")
    public RespBean dailyInfo(@RequestParam("date") String date,
                              @RequestAttribute("sno") String sno) {
        return RespBean.success(recordService.dailyInfo(sno, date));
    }

    /**
     * 展示某天班级排名
     *
     * @param date
     * @param sno
     * @param classroom
     * @return
     */
    @AccessLimit
    @GetMapping("/getRowName")
    public RespBean getRowName(@RequestParam("date") String date,
                               @RequestAttribute("sno") String sno,
                               @RequestParam("classroom") String classroom) {
        return RespBean.success(recordService.getRowName(sno, date, classroom));
    }

    /**
     * 展示班级学习平均情况
     *
     * @param date
     * @param classroom
     * @return
     */
    @AccessLimit
    @GetMapping("/classStudy")
    public RespBean getClassStudy(@RequestParam("date") String date,
                                  @RequestParam("classroom") String classroom) {
        return RespBean.success(recordService.getClassStudy(date, classroom));
    }
}
